package com.jackrain.nea.fc.vp.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.utils.Constants;
import com.jackrain.nea.fc.vp.api.ViewCvipUpRollBackCmd;
import com.jackrain.nea.fc.vp.mapper.ViewCvipUpMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * FC-会员接口-VIP升降级-回写ISREAD字段判断是否传输成功
 *
 * @author WSL
 * @create 2018/1/17
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "vp")
public class ViewCvipUpRollBackCmdImpl extends CommandAdapter implements ViewCvipUpRollBackCmd {

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        ValueHolder vh = new ValueHolder();
        // Mapper
        ViewCvipUpMapper viewCvipUpMapper = ApplicationContextHandle.getBean(ViewCvipUpMapper.class);

        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        log.info("ViewCvipUpRollBackCmdImpl--param-->{}",param);

        if (null == param) {
            throw new NDSException("参数格式错误！");
        }

        int count = viewCvipUpMapper.updateIsRead(param);
        if (count < 0){
            vh.put("code",-1);
        }else{
            vh.put("code",0);
            vh.put("dataCount",count);
//            log.debug("VIP-RollBack-dataCount:"+count);
        }
        return vh;
    }
}
